
import {
  _decorator, Component, PhysicsSystem2D, EPhysics2DDrawFlags,
  Contact2DType, Collider2D, IPhysics2DContact, UITransform
} from 'cc';
const { ccclass, property } = _decorator;

@ccclass('ContartCallBack')
export class ContartCallBack extends Component {
  // [1]
  // dummy = '';

  // [2]
  // @property
  // serializableDummy = 0;

  start() {
    // [3]
    const instance = PhysicsSystem2D.instance;
    console.log(instance);

    instance.debugDrawFlags
      //  = EPhysics2DDrawFlags.None;
      = EPhysics2DDrawFlags.Aabb |
      EPhysics2DDrawFlags.Pair |
      EPhysics2DDrawFlags.CenterOfMass |
      EPhysics2DDrawFlags.Joint |
      EPhysics2DDrawFlags.Shape

    // 物理步长，默认 fixedTimeStep 是 1/60
    instance.fixedTimeStep = 1 / 30;
    // 每次更新物理系统处理速度的迭代次数，默认为 10
    instance.velocityIterations = 8;
    // 每次更新物理系统处理位置的迭代次数，默认为 10
    instance.positionIterations = 8;
    // 注册全局碰撞回调函数
    if (instance) {
      instance.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
      instance.on(Contact2DType.END_CONTACT, this.onEndContact, this);
      instance.on(Contact2DType.PRE_SOLVE, this.onPreSolve, this);
      instance.on(Contact2DType.POST_SOLVE, this.onPostSolve, this);
    }
  }


  onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact) {
    // 只在两个碰撞体开始接触时被调用一次
    console.log('onBeginContact');
  }
  onEndContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact) {
    // 只在两个碰撞体结束接触时被调用一次
    console.log('onEndContact');
  }
  onPreSolve(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact) {
    // 每次将要处理碰撞体接触逻辑时被调用
    // console.log('onPreSolve');
  }
  onPostSolve(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact) {
    // 每次处理完碰撞体接触逻辑时被调用
    // console.log('onPostSolve');
  }

  // update (deltaTime: number) {
  //     // [4]
  // }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.0/manual/en/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.0/manual/en/scripting/ccclass.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.0/manual/en/scripting/life-cycle-callbacks.html
 */
